package com.course02.order_backend.service.llh;

import com.course02.order_backend.entity.llh.Dish;
import com.course02.order_backend.entity.llh.Table;
import com.course02.order_backend.exception.llh.BusinessException;
import com.course02.order_backend.exception.llh.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class OrderServiceImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;//已引入的连接池,这个作为来调用数据库去执行某些方法
    public void createOrder(Integer tableId,Integer dishId,Integer quantity){
        Table table=jdbcTemplate.queryForObject("Select id,status from t_table where id=?",//通过查询语句获取数据库的数据
                (rs, rowNum) -> {
            Table t=new Table();
            t.setId(rs.getInt("id"));
            t.setStatus(rs.getInt("status"));
            return t;
        },
        tableId);
        if(tableId==null){//查询到table没有这个对象，那就抛出异常告诉用户没有这个桌号
            //抛出自定义异常BusinessException没有桌号的枚举
            throw new BusinessException(ErrorCode.TABLE_NOT_EXIST,"桌号="+tableId);
        }
        //校验桌号是否已被占用,1表示桌号被占用
        if(table.getStatus()==1){
            throw new BusinessException(ErrorCode.TABLE_ALREADY_OCCUPIED,"桌号"+tableId);
        }
        //校验菜品是否存在或者菜品库存是否足够
        Dish dish=jdbcTemplate.queryForObject("SELECT id,stock,status FROM t_dish WHERE id=?",
                (rs, rowNum) -> {
                    Dish d=new Dish();
                    d.setId(rs.getInt("id"));
                    d.setStatus(rs.getInt("status"));
                    return d;
                },
                dishId);
                //判断如果没有获取到菜品
                if(dish==null){
                    throw new BusinessException(ErrorCode.DISH_NOT_EXIST,"菜品"+dishId);//菜品不存在
                }
                if(dish.getStatus()==0){
                    throw new BusinessException(ErrorCode.DISH_OFF_SHELF,"菜品"+dishId);//菜品下架情况
                }
                if(dish.getStock()<quantity){
                    throw new BusinessException(ErrorCode.DISH_STOCK_INSUFFICIENT,"菜品="+dishId+"库存："+dish.getStock()+"需要："+quantity);//菜品库存不足
                }
                //扣库存就是消费了点了实时菜品数量减少
                int updateStock=jdbcTemplate.update("UPDATE t_dish SET stock=stock-? where id=?",quantity,dishId);
                if(updateStock!=1){//影响行数不等于1，说明扣库存失败
                    throw new BusinessException(ErrorCode.ORDER_CREATE_FAIL,"扣库存失败,菜品id："+dishId);
                }
                //创建订单
                BigDecimal quantityBigDecimal=new BigDecimal(quantity);
                BigDecimal totalPrice=dish.getPrice().multiply(quantityBigDecimal);
                int insertOrder=jdbcTemplate.update("INSERT INTO t_order(table_id,dish_id,quantity,total_price) VALUES(?,?,?,?)",
                        tableId,dishId,quantity,totalPrice
                        );
                if(insertOrder!=1){
                    throw new BusinessException(ErrorCode.ORDER_CREATE_FAIL,"订单创建失败,桌号："+tableId);
                }
    }

}
